﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rose.Models;
using Rose.Helper;

namespace Rose.Controllers
{
    [Authorize]
    public class ShareController : Controller
    {
        //
        // GET: /Share/
        private ShareHelpModel help = new ShareHelpModel();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AddShare()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AnalyseShare (string link)
        {
            //处理本站分享的问题;

            if (string.IsNullOrEmpty(link))
                return Json(new { description = "请输入链接", link = link }, JsonRequestBehavior.AllowGet);

            WormShareContentHelpModel help=new WormShareContentHelpModel();
            string returnstring = help.AnalyseLink(link);
            if (!string.IsNullOrEmpty(returnstring))
                return Json(new { description = returnstring, link = link }, JsonRequestBehavior.AllowGet);

            else if (help.IsLinkUseless)
                return Json(new { description = Rose.ViewRes.Defaultresx.DefaultShareUseless, link = link }, JsonRequestBehavior.AllowGet);

            return Json(new { description = Rose.ViewRes.Defaultresx.DefaultShareContent ,link=link},JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public ActionResult AddShare(string link,string description)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();

            
            if (help.AddShareToDatabase(selfId, link, description))
            {
                return Content("分享成功");
            }

            else return Content("分享失败"); 

        }
        [HttpPost]
        public ActionResult DeleteShare(string id)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            if (help.RemoveShareFromDatabase(id, selfId))
                return Content("删除分享成功");
            return Content("删除分享失败");
        }

        public ActionResult ShowShareList(int currentpage=1)
        {
            
            const int PAGESIZE=20;
            string selfId = new CheckHelpModel().CheckCurrentUserId();
             var model=help.GetShareList(selfId, currentpage, PAGESIZE);
            int allcount= help.GetShareCount(selfId);
             ViewData["AllCount"] =allcount;
             ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;

             return View(model);
        }
        
    }
}
